/**
 * problem_005.c
 * Copyright (C) 2011-03-18 - xrose
 * The smallest positive number that is evenly divisible by all of the numbers
 * from 1 to 20?
 */

#include <stdio.h>

int main (int argc, char *argv[])
{
    int val = 23;
    long long i, j;
    int step = prodPrime(val);
    printf("val: %d\n", val);
    printf("step: %d\n", step);
    //--
    printf("isPrime: %d\n", isPrime(2));
    for(i = step;;i=i+step)
    {
        for (j=1; j<=val; j++)
        {
            if(i%j != 0)
            {
                printf("debug: %ld   %ld\n", i, j);
                break;
            }
        }
        if(j==val+1)
        {
            printf("The smallest number divisible from 1 to 22: %d\n", i);
            break;
        }
    }
    return 0;
}

int prodPrime(int val)
{
    int prod = 1;
    int i;
    for(i=1; i<=val; i++)
    {
        if(isPrime(i))
        {
            prod = prod * i;
        }
    }
    return prod;
}
/*is a prime?*/
int isPrime(long numb)
{
    int sqrtnumb = floor(sqrt(numb));
    int i;
    if(numb == 2)
        return 1;
    else
        if(numb%2 == 0) return 0;
    for(i = 3; i<=sqrtnumb; i=i+2)
    {
        if(numb%i == 0)
        {
            return 0;
        }
    }
    return 1;
}
